package shadow;



import intersection.GroupIntersection;
import primitives.World3D;
import ray.Ray;
import light.Light;
import vector.Vector;

/**
 * 
 * Provide calculation for shadowing. If input argument enables shadowing, this
 * class will be used for calculating.
 * 
 * author Yuting Wu
 * 
 * @author Kan Lin
 * @author Bing Dong
 * 
 */
public class ShadowsCalculation {

	private Vector pHitPoint;
	private World3D world;

	/**
	 * @param pHitPoint
	 * @param world
	 */
	public ShadowsCalculation(Vector pHitPoint, World3D world) {
		super();
		this.pHitPoint = pHitPoint;
		this.world = world;
	}

	/**
	 * judge whether there are an object blocking the light
	 * 
	 * @param singleLight
	 * @param insideOrOutside
	 * @param shape
	 * @return cos value if no hit return 0.0
	 */
	public double shadowJudge(Light singleLight, boolean ifshadow) {
		if (ifshadow) {
			GroupIntersection gHit = new GroupIntersection(world);

			// judge whether a light is hidden
			Ray ray = new Ray(singleLight.getLight(), pHitPoint.subtract(
					singleLight.getLight()).normalise());

			boolean ifHit = gHit.calMutiIntersection(ray);

			// there exists hitpoint
			if (ifHit) {
		
				Vector newHitPoint = gHit.getpHitpoint();

				// check whether it is the hitpoint itself on the way bewteen
				// the light and hitpoint
				// if not returns 0 shows no colour
				// if yes continues the colour calculation of the object
				if (!pHitPoint.objectEqual(newHitPoint)) {
					return 0.0;
				}
			}
			return 1.0;
		}else{
			return 1.0;
		}
	}

}
